package com.example.common.widget

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.example.common.R
import com.example.common.base.BaseDialog

class DialogMessage : BaseDialog {


    private var mview: View? = null

    private var tv_determine: TextView? = null
    private var tv_cancel: TextView? = null
    private var tv_title: TextView? = null
    private var tv_msg: TextView? = null

    companion object {
        fun build(context: Context): DialogMessage {
            return DialogMessage(context)
        }
    }

    constructor(
        context: Context
    ) : super(context) {
        initView()
    }

    override fun initView() {
        mview = LayoutInflater.from(context)
            .inflate(R.layout.diaolog_message, null)
        tv_determine = mview!!.findViewById(R.id.tv_determine)
        tv_title = mview!!.findViewById(R.id.tv_title)
        tv_msg = mview!!.findViewById(R.id.tv_msg)
        tv_cancel = mview!!.findViewById(R.id.tv_cancel)
    }

    override fun layout() = mview!!


    fun setTitle(title: String): DialogMessage {
        tv_title?.text = title
        return this
    }

    fun setMessage(msg: String): DialogMessage {
        tv_msg?.text = msg
        return this
    }

    fun setDetermine(determine: String, listener: View.OnClickListener): DialogMessage {
        tv_determine?.text = determine
        tv_determine?.setOnClickListener(listener)
        return this
    }

    fun setDetermineColor(color: Int): DialogMessage {
        tv_determine?.setTextColor(color)
        return this
    }

    fun setCancelColor(color: Int): DialogMessage {
        tv_cancel?.setTextColor(color)
        return this
    }

    fun setCancel(cancel: String, listener: View.OnClickListener): DialogMessage {
        tv_cancel?.text = cancel
        tv_cancel?.setOnClickListener(listener)
        return this
    }

    fun setCancel(cancel: String): DialogMessage {
        tv_cancel?.text = cancel
        tv_cancel?.setOnClickListener {
            dismiss()
        }
        return this
    }

    fun showView(): DialogMessage {
        show()
        return this
    }

    fun setOnCancelable(flag:Boolean): DialogMessage{
        setCancelable(flag)
        return this
    }

    override fun show() {
        super.show()
//        val layoutParams = window!!.attributes
//        layoutParams.gravity = Gravity.CENTER
////        layoutParams.alpha = 0.7f //设置阴影透明度
//        layoutParams.flags = (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
//                or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
//                or WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL)
//        layoutParams.gravity = Gravity.CENTER
//        layoutParams.format = PixelFormat.TRANSLUCENT
//        layoutParams.width = DensityUtil.dip2px(context, 270f).toInt()
//        window!!.attributes = layoutParams

    }

}



